<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('主子表提交')"/>
    <th:block th:include="include :: datetimepicker-css"/>
    <th:block th:include="include :: select2-css"/>
    <style>
        input::-webkit-outer-spin-button,
        input::-webkit-inner-spin-button {
            -webkit-appearance: none;
        }

        input[type="number"] {
            -moz-appearance: textfield;

        }

    </style>

</head>


<body class="gray-bg">

<div class="main-content">
    <form id="form-edit" class="form-horizontal">
        <div class="row">
            <div class="col-sm-12">
                <div class="col-sm-12 select-table table-striped">
                    <table id="bootstrap-table"></table>
                </div>
            </div>
        </div>
    </form>
</div>

<div class="row">
    <div class="col-sm-offset-5 col-sm-10">
        <button type="button" class="btn btn-sm btn-primary" onclick="submitHandler()"><i class="fa fa-check"></i>保 存
        </button>&nbsp;
        <button type="button" class="btn btn-sm btn-danger" onclick="closeItem()"><i class="fa fa-reply-all"></i>关 闭
        </button>
    </div>
</div>

<th:block th:include="include :: footer"/>
<th:block th:include="include :: datetimepicker-js"/>
<th:block th:include="include :: select2-js"/>
<script th:src="@{/js/jquery.tmpl.js}"></script>
<script th:inline="javascript">
    var prefix = ctx + "biPlan";
    /* 初始化 数据 */
    var data = [[${plan}]];
    var years = [[${years}]];
    var months = [[${months}]];
    var compName = [[${comp}]];
    $(function () {
        var options = {
            data: data,
            pagination: false,
            showSearch: false,
            showRefresh: false,
            showToggle: false,
            showColumns: false,
            sidePagination: "client",
            columns: [
                {
                    field: 'index',
                    align: 'center',
                    title: "序号",
                    formatter: function (value, row, index) {
                        var html = "<input class='form-control' readonly type='hidden' name='con[%s].id'  value='" + row.id + "'>";
                        return $.common.sprintf(html, index, value) + (index + 1);
                    }
                },
                {
                    field: 'compName',
                    align: 'center',
                    title: "工程公司",
                    formatter: function (value, row, index) {
                        var html = "<input class='form-control' readonly type='text' name='con[%s].compName' id='compName" + index + "' value='" + row.compName + "'>";
                        return $.common.sprintf(html, index, value);
                    }
                },
                {
                    field: 'proName',
                    align: 'center',
                    title: "项目名称",
                    formatter: function (value, row, index) {
                        var html = "<input class='form-control' readonly type='text' name='con[%s].proName' id='proName" + index + "' value='" + row.proName + "'>";
                        return $.common.sprintf(html, index, value);
                    }
                },
                {
                    field: 'years',
                    align: 'center',
                    title: "年份",
                    formatter: function (value, row, index) {
                        var html = '';
                        html += '<select class="form-control" name="con[%s].years" id="years' + index + '" onchange="getTime(' + index + ')"><option value="">请选择</option>';
                        for (var i = 0; i < years.length; i++) {
                            html += '<option value=' + years[i].years
                            if (years[i].years == value) {
                                html += ' selected ';
                            }
                            html += '>' + years[i].years + '年</option>'
                        }
                        html += '</select>';
                        return $.common.sprintf(html, index, value);
                    }

                },
                {
                    field: 'months',
                    align: 'center',
                    title: "月份",
                    formatter: function (value, row, index) {
                        var html = '';
                        html += '<select class="form-control" name="con[%s].months" id="months' + index + '" onchange="getTime(' + index + ')"><option value="">请选择</option>';
                        for (var i = 0; i < months.length; i++) {
                            html += '<option value=' + months[i].monthInYear
                            if (months[i].monthInYear == value) {
                                html += ' selected ';
                            }
                            html += '>' + months[i].monthInYear + '月</option>'
                        }
                        html += '</select>';
                        return $.common.sprintf(html, index, value);
                    }

                },
                {
                    field: 'planOutputValue',
                    align: 'center',
                    title: "计划产值",
                    formatter: function (value, row, index) {
                        var html = $.common.sprintf("<input class='form-control' type='number' placeholder='请输入计划产值' name='con[%s].planOutputValue' value='%s' autocomplete=\"off\" required>", index, value);
                        return html;
                    }

                },
                {
                    field: 'realityOutputValue',
                    align: 'center',
                    title: "实际产值",
                    formatter: function (value, row, index) {
                        var html = $.common.sprintf("<input class='form-control' type='number' placeholder='请输入实际产值' name='con[%s].realityOutputValue' value='%s' autocomplete=\"off\" required>", index, value);
                        return html;
                    }
                },
                {
                    field: 'planBackPay',
                    align: 'center',
                    title: "计划回款",
                    formatter: function (value, row, index) {
                        var html = $.common.sprintf("<input class='form-control' type='number' placeholder='请输入计划回款' name='con[%s].planBackPay' value='%s' autocomplete=\"off\" required>", index, value);
                        return html;
                    }
                },
                {
                    field: 'realityBackPay',
                    align: 'center',
                    title: "实际回款",
                    formatter: function (value, row, index) {
                        var html = $.common.sprintf("<input class='form-control' type='number' placeholder='请输入实际回款' name='con[%s].realityBackPay' value='%s' autocomplete=\"off\" required>", index, value);
                        return html;
                    }
                },
            ]
        };
        $.table.init(options);
        $("select").select2();
    });

    function getTime(index) {
        sub.editColumn();
        var data = $('#bootstrap-table').bootstrapTable('getData');
        if ($("#years" + index).val() != "" && $("#months" + index).val() != "" && $("#compName" + index).val() != "" && $("#proName" + index).val() != "") {
            for (var i = 0; i < data.length; i++) {
                if (i != index) {
                    if ($("#years" + index).val() == data[i].years && $("#months" + index).val() == data[i].months && $("#compName" + index).val() == data[i].compName && $("#proName" + index).val() == data[i].proName) {
                        $("#months" + index).val("").select2();
                        $.modal.alertWarning("存在重复时间，请重新选择！");
                    }
                }
            }
        }
        $("select").select2();
    }

    function judge(index) {
        if ($("#compName" + index).val() == "" || $("#proName" + index).val() == "" || $("#years" + index).val() == "" || $("#months" + index).val() == "") {
            return false
        }
        return true
    }

    function validform(formId) {
        return $(formId).validate()
    }

    function submitHandler() {
        if (validform("#form-edit").form()) {
            var dataList = $('#bootstrap-table').bootstrapTable('getData');
            var data = $("#form-edit").serializeArray();
            for (var i = 0; i < dataList.length; i++) {
                if (!judge(i)) {
                    $.modal.alertWarning("下拉框不能为空！");
                    $("select").select2();
                    return false
                }
            }
            $.operate.saveTab(prefix + '/edit', data)
        }

    }
</script>
</body>
</html>
